package com.atguigu.stuty.interactive;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author early morning
 * @Create 2025/4/23 19:38
 * @Description: 线程间的通信
 * 多线程之间按顺序调用，实现A->B->C
 * 三个线程启动，要求如下：
 * AA打印5次，BB打印10次，CC打印15次
 * 接着
 * AA打印5次，BB打印10次，CC打印15次
 * .....来10轮
 *
 *  Java里面如何进行工程级别的多线程编写
 *  1 多线程变成模板（套路）
 *    高内聚低耦合前提下，线程    操作    资源类
 *  2 判断   干活   通知
 *  3 多线程通信中，如果存在wait方法的调用要避免，------虚假唤醒
 */

class ShareData {
    private int flag = 1; // 标记位 A：1 B：2 C：3
    // 锁
    private Lock lock = new ReentrantLock();
    // 钥匙
    private Condition condition = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();


    public void print5() throws InterruptedException {
        // 开启锁
        lock.lock();
        try {
            // 1.判断
            while (flag != 1) {
                condition.await();
            }
            // 2.干活
            for (int i = 1; i <= 5; i++) {
                System.out.println(Thread.currentThread().getName()+ "\t" + i);
            }
            // 3.通知
            flag = 2;
            condition2.signal();
        } finally {
            lock.unlock();
        }
    }
    public void print10() throws InterruptedException {
        // 开启锁
        lock.lock();
        try {
            // 1.判断
            while (flag != 2) {
                condition2.await();
            }
            // 2.干活
            for (int i = 1; i <= 10; i++) {
                System.out.println(Thread.currentThread().getName()+ "\t" + i);
            }
            // 3.通知
            flag = 3;
            condition3.signal();
        } finally {
            lock.unlock();
        }
    }
    public void print15() throws InterruptedException {
        // 开启锁
        lock.lock();
        try {
            // 1.判断
            while (flag != 3) {
                condition3.await();
            }
            // 2.干活
            for (int i = 1; i <= 15; i++) {
                System.out.println(Thread.currentThread().getName()+ "\t" + i);
            }
            // 3.通知
            flag = 1;
            condition.signal();
        } finally {
            lock.unlock();
        }
    }

}


public class ThreadOrderAccess {
    public static void main(String[] args) {
        // 创建资源类
        ShareData shareData = new ShareData();

        new Thread(() -> {
            // Runnable 接口的实现类
            for (int i = 0; i < 10; i++) {
                try {
                    shareData.print5();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"A").start();
        new Thread(() -> {
            // Runnable 接口的实现类
            for (int i = 0; i < 10; i++) {
                try {
                    shareData.print10();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"B").start();
        new Thread(() -> {
            // Runnable 接口的实现类
            for (int i = 0; i < 10; i++) {
                try {
                    shareData.print15();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"C").start();
    }
}
